home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / c / gcc / gcc258s.zoo / getpwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-30  |  2.1 KB  |  98 lines

  1. /* getpwd.c - get the working directory */
  2.  
  3. #include "config.h"
  4.  
  5. #include <errno.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8.  
  9. #ifndef errno
  10. extern int errno;
  11. #endif
  12.  
  13. /* Virtually every UN*X system now in common use (except for pre-4.3-tahoe
  14.    BSD systems) now provides getcwd as called for by POSIX.  Allow for
  15.    the few exceptions to the general rule here.  */
  16.  
  17. #if !(defined (POSIX) || defined (USG) || defined (VMS) || defined(atarist) || defined(CROSSHPUX))
  18. #include <sys/param.h>
  19. extern char *getwd ();
  20. #define getcwd(buf,len) getwd(buf)
  21. #define GUESSPATHLEN (MAXPATHLEN + 1)
  22. #else /* (defined (USG) || defined (VMS)) */
  23. extern char *getcwd ();
  24. /* We actually use this as a starting point, not a limit.  */
  25. #define GUESSPATHLEN 100
  26. #endif /* (defined (USG) || defined (VMS)) */
  27.  
  28. char *getenv ();
  29. char *xmalloc ();
  30.  
  31. #ifndef VMS
  32.  
  33. /* Get the working directory.  Use the PWD environment variable if it's
  34.    set correctly, since this is faster and gives more uniform answers
  35.    to the user.  Yield the working directory if successful; otherwise,
  36.    yield 0 and set errno.  */
  37.  
  38. char *
  39. getpwd ()
  40. {
  41.   static char *pwd;
  42.   static int failure_errno;
  43.  
  44.   char *p = pwd;
  45.   size_t s;
  46.   struct stat dotstat, pwdstat;
  47.  
  48.   if (!p && !(errno = failure_errno))
  49.     {
  50.       if (! ((p = getenv ("PWD")) != 0
  51. #ifndef atarist
  52.          && *p == '/'
  53.          && stat (p, &pwdstat) == 0
  54.          && stat (".", &dotstat) == 0
  55.          && dotstat.st_ino == pwdstat.st_ino
  56.          && dotstat.st_dev == pwdstat.st_dev
  57. #endif
  58.          ))
  59.  
  60.     /* The shortcut didn't work.  Try the slow, ``sure'' way.  */
  61.     for (s = GUESSPATHLEN;  ! getcwd (p = xmalloc (s), s);  s *= 2)
  62.       {
  63.         int e = errno;
  64.         free (p);
  65. #ifdef ERANGE
  66.         if (e != ERANGE)
  67. #endif
  68.           {
  69.         errno = failure_errno = e;
  70.         p = 0;
  71.         break;
  72.           }
  73.       }
  74.  
  75.       /* Cache the result.  This assumes that the program does
  76.      not invoke chdir between calls to getpwd.  */
  77.       pwd = p;
  78.     }
  79.   return p;
  80. }
  81.  
  82. #else    /* VMS */
  83.  
  84. #ifndef MAXPATHLEN
  85. #define MAXPATHLEN 255
  86. #endif
  87.  
  88. char *
  89. getpwd ()
  90. {
  91.   static char *pwd = 0;
  92.  
  93.   if (!pwd) pwd = getcwd (xmalloc (MAXPATHLEN+1), MAXPATHLEN+1);
  94.   return pwd;
  95. }
  96.  
  97. #endif    /* VMS */
  98.